package com.example.controller.user;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.pojo.dto.PageSearchDto;
import com.example.pojo.dto.UserSignUpDto;
import com.example.pojo.entity.User;
import com.example.pojo.entity.UserProfile;
import com.example.properties.JwtProperties;
import com.example.result.Result;
import com.example.service.IUserService;
import com.example.utils.JwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 请求参数有大括号的参数需要用PathVariable,
 * 涉及时间的需要@DateTimeFormat(pattern = "yyyy-MM-dd")设置格式
 * 当响应数据为json格式需要使用Requestbody,使用 @RequestParam 从请求中提取查询参数、表单参数甚至是多个参数
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private JwtProperties jwtProperties;
 HashMap<String,String> map=new HashMap<>();
    //登录
    @PostMapping("/login")
    public Result handleSignIn(@RequestBody User user, HttpServletResponse httpServletResponse) {
        System.out.println(user.toString());
        String token = null;
        //用户通过邮箱或者用户名登录
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (user.getEmail().equals("")) {
            lambdaQueryWrapper.eq(User::getName, user.getName());
            User user1 = userService.getOne(lambdaQueryWrapper);
            if (userService.getOne(lambdaQueryWrapper) != null) {
                Map<String, Object> claims = new HashMap<>();
                claims.put("userId", user1.getId());
                token = JwtUtil.createJWT(
                        jwtProperties.getUserSecretKey(),
                        jwtProperties.getUserTtl(),
                        claims);
                Cookie cookie=new Cookie("token",token);
                cookie.setPath("/");
                cookie.setMaxAge(3600);
                cookie.setHttpOnly(true);
                httpServletResponse.addCookie(cookie);
            }
        } else {
            lambdaQueryWrapper.eq(User::getEmail, user.getEmail());
            User user2 = userService.getOne(lambdaQueryWrapper);
            if (userService.getOne(lambdaQueryWrapper) != null) {
                Map<String, Object> claims = new HashMap<>();
                claims.put("userId", user2.getId());
                token = JwtUtil.createJWT(
                        jwtProperties.getUserSecretKey(),
                        jwtProperties.getUserTtl(),
                        claims);
                Cookie cookie=new Cookie("token",token);
                httpServletResponse.addCookie(cookie);
            }
        }
        return Result.success(token);
    }

    //注册
    @PostMapping("/signUp")
    public Result handleSignUp(@RequestBody UserSignUpDto userSignUpDto) {
      try {
          System.out.println(userSignUpDto);
          String password = userSignUpDto.getPassword();
        String repeatPassword = userSignUpDto.getRepeatPassword();
        if(!password.equals(repeatPassword)){
          return   Result.error("两次密码不一致");
        }
          String s = map.get(userSignUpDto.getEmail());
          System.out.println(s);
        if(!s.equals(userSignUpDto.getCode())){
            return  Result.error("验证码输入不正确");
        }
        User user = new User();
        BeanUtils.copyProperties(userSignUpDto, user);

        userService.save(user);

        return Result.success("注册成功");
      } catch ( Exception e){
          return null;
      }
    }
    //发送验证码
    @PostMapping("/code")
    public Result createCode(@RequestBody Map<String,String> map1){
        Random random = new Random();
       String code= String.valueOf(1000 + random.nextInt(9000));
       map.put(map1.get("email"),code);
        System.out.println(map1);
        System.out.println(code);
       return Result.success(code);
    }

}
